package cn.zhangmh.easymvp.books.ui;

import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import cn.zhangmh.easymvp.R;
import cn.zhangmh.easymvp.books.entity.BookBean;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link BookInfoFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link BookInfoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BookInfoFragment extends Fragment {



    private OnFragmentInteractionListener mListener;

    private ImageView imageView;
    private TextView book_title;
    private  TextView book_summary;
    private  TextView book_catalog;
    private TextView book_author;
    private  TextView author_intro;


    private  static BookBean book;

    public BookInfoFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     * @return A new instance of fragment BookInfoFragment.
     */
    public static BookInfoFragment newInstance(BookBean mbook) {
        BookInfoFragment fragment = new BookInfoFragment();
        book = mbook;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_book_info, container, false);
        imageView = (ImageView)view.findViewById(R.id.image_main);
        book_title = (TextView)view.findViewById(R.id.book_title);
        book_summary = (TextView) view.findViewById(R.id.book_summary);
        book_catalog = (TextView) view.findViewById(R.id.book_catalog);
        book_author = (TextView) view.findViewById(R.id.book_author);
        author_intro = (TextView) view.findViewById(R.id.author_intro);
        ImageLoader.getInstance().displayImage(book.getImages().getLarge(),imageView);
        book_title.setText(book.getTitle());
        author_intro.setText(book.getAuthor_intro());
        book_author.setText(book.getAuthor().toString());
        book_summary.setText(book.getSummary());
        book_catalog.setText(book.getCatalog());
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed() {
        if (mListener != null) {
            mListener.onFragmentInteraction();
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction();
    }
}
